package com.ruoyi.api.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.user.dto.*;
import com.ruoyi.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;

@Api(tags = "用户中心")
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends ApiBaseController{

    @Autowired
    private IUserService userService;

    /**
     * 注册用户
     * @param registerDTO
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public AjaxResult register (@Valid UserRegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }

    /**
     * 注册用户之前的通过验证码
     * @param registerForwardDTO
     */
    @PostMapping("/register/forward")
    @ApiOperation("用户注册(需要通过验证码)")
    public AjaxResult registerForward (@Valid UserRegisterForwardDTO registerForwardDTO) {
        return userService.registerForward(registerForwardDTO);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/send/verifycode")
    @ApiOperation("发送验证码")
    public AjaxResult sendVerifyCode (@Valid SendCodeDTO sendCodeDTO) {
        return userService.sendVerifyCode(sendCodeDTO);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public AjaxResult login (@Valid LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public AjaxResult logout (@Valid LogoutDTO logoutDTO) {
        checkLogin();
        return userService.logout(logoutDTO);
    }

    @PostMapping("/findpassword/forward")
    @ApiOperation("找回密码(需要通过验证码)")
    public AjaxResult findPasswordForward(@Valid FindPasswordForward passwordForward) {
        return userService.findPasswordForward(passwordForward);
    }

    @PostMapping("/findpassword")
    @ApiOperation("找回密码")
    public AjaxResult findPassword(@Valid FindPassword findPassword) {
        return userService.findPassword(findPassword);
    }

    @PostMapping("/update/password")
    @ApiOperation("修改密码")
    public AjaxResult updatePassword (@Valid UpdatePasswordDTO updatePasswordDTO) {
        checkLogin();
        return userService.updatePassword(updatePasswordDTO);
    }

    @PostMapping("/update/nickname")
    @ApiOperation("修改昵称")
    public AjaxResult updateNickName (@Valid UpdateNicknameDTO updateNicknameDTO) {
        checkLogin();
        return userService.updateNickName(updateNicknameDTO);
    }

    @PostMapping(value = "/upload/avatar", consumes = "multipart/form-data")
    @ApiOperation("上传头像")
    public AjaxResult uploadAvatar(UploadAvatarDTO uploadAvatarDTO){
        checkLogin();
        return userService.uploadAvatar(uploadAvatarDTO);
    }

    @PostMapping("/info")
    @ApiOperation("获取当前最新的用户信息")
    public AjaxResult getUserInfo(){
        ApiUserVo apiUserVo = checkLoginAndGetMember();
        return AjaxResult.success(apiUserVo);
    }
}
